package kr.zetaomicron.ultrasolitaire;

import kr.zetaomicron.framework.math.Vector2;
import android.graphics.PointF;

import android.util.Log;

class MoveCard {

  private static final int MAX_CARDS = 104;

  private boolean mValid;
  private Card[] mCard;
  private int mCardCount, mLastCardCount, mLastCardAnchorIdx, mNewCardAnchorIdx;
  private CardAnchor mCardAnchor;
	private Vector2 mOriginalPoint, mCurPoint;

	public MoveCard() {
		mCard = new Card[MAX_CARDS];
		mOriginalPoint = new Vector2(1, 1);
		mCurPoint = new Vector2(1, 1);
		mLastCardAnchorIdx = -1;	// invalid index
		Clear();
	}

  public boolean IsValid() { return mValid; }
	public CardAnchor GetAnchor() { return mCardAnchor; }
	public int GetAnchorIdx(boolean newIdx) {	return newIdx ? mNewCardAnchorIdx : mLastCardAnchorIdx;	}
	public int GetCount() {	return mCardCount;	}
	public int GetLastCount() {	return mLastCardCount;	}
  public Card GetTopCard() { return mCard[0]; }
  	public Card[] GetCards() {	return mCard;	}
  	public Vector2 GetOriginalPoint() {	return mOriginalPoint;	}
  	public Vector2 GetCurrentPoint() {	return mCurPoint;	}
	public void SetAnchor(CardAnchor anchor, int idx) { 
		mCardAnchor = anchor;	mLastCardAnchorIdx = idx;
	}
	public void SetNewCardAnchorIdx(int newIdx) {	mLastCardAnchorIdx = newIdx;	}

//  public void Draw(DrawMaster drawMaster, Canvas canvas) {
//    for (int i = 0; i < mCardCount; i++) {
//      drawMaster.DrawCard(canvas, mCard[i]);
//    }
//  }

  private void Clear() {
    mValid = false;
    mCardCount = 0;
    mCardAnchor = null;
    for (int i = 0; i < MAX_CARDS; i++) {
      mCard[i] = null;
    }
  }

	public void Release() {
		if (mValid) {
			mValid = false;
			Log.i("[MoveCard]", "[Release] valid anim Anchor: " + String.valueOf(mLastCardAnchorIdx) + ", count: " + String.valueOf(mCardCount));
			Settings.setCardAnim(-1, mLastCardAnchorIdx, mCardCount, true);
			for (int i = 0; i < mCardCount; i++) {
				mCardAnchor.AddCard(mCard[i]);
			}
			Clear();
		} else {
			//Settings.setCardAnim(-1, mLastCardAnchorIdx, mCardCount, true);
		}
	}

	public void AddCard(Card card) {
  		if (mCardCount == 0) {
  			mOriginalPoint.set(card.GetX(), card.GetY());
  			mLastCardCount = 0;
  		}
  		mCard[mCardCount++] = card;
  		++mLastCardCount;
  		mValid = true;
	}

  public void MovePosition(float dx, float dy) {
    for (int i = 0; i < mCardCount; i++) {
      mCard[i].MovePosition(dx, dy);
    }
  }
  
	public void SetPosition(float x, float y) {
		for (int i = 0; i < mCardCount; i++) {
			mCard[i].SetPosition(x - Card.WIDTH / 4 * i, y);
		}
		mCurPoint.x = mCard[mCardCount - 1].GetX();
		mCurPoint.y = mCard[mCardCount - 1].GetY();
		//mCurPoint.x = mCard[0].GetX();
		//mCurPoint.y = mCard[0].GetY();
	}


  public Card[] DumpCards() {
    return DumpCards(true);
  }

	public Card[] DumpCards(boolean unhide) {
		Card[] ret = null;
		if (mValid) {
			mValid = false;
			if (unhide) {
				mCardAnchor.UnhideTopCard();
			}
			ret = new Card[mCardCount];
			for (int i = 0; i < mCardCount; i++) {
				ret[i] = mCard[i];
			}
			//Log.i("[MoveCard]", "[DumpCards] valid anim Anchor: " + String.valueOf(mLastCardAnchorIdx) + ", count: " + String.valueOf(mCardCount));
			Settings.setCardAnim(-1, mLastCardAnchorIdx, mCardCount, true);
			Clear();
		}
		return ret;
	}

	public void InitFromSelectCard(SelectCard selectCard, int idx, float x, float y) {
		int count = selectCard.GetCount();
		mCardAnchor = selectCard.GetAnchor();
		mLastCardAnchorIdx = idx;
		Card[] cards = selectCard.DumpCards();

		for (int i = 0; i < count; i++) {
			cards[i].SetPosition(x - Card.WIDTH/2, y - Card.HEIGHT/2 + 15*i);
			AddCard(cards[i]);
		}
		mValid = true;
	}

	public void InitFromAnchor(CardAnchor cardAnchor, int idx, float x, float y) {
		mCardAnchor = cardAnchor;
		mLastCardAnchorIdx = idx;
		Card[] cards = cardAnchor.GetCardStack();
		//Log.i("[MoveCard]", "[InitFromAnchor] cards == " + String.valueOf(cards.length));

		for (int i = 0; i < cards.length; i++) {
			cards[i].SetPosition(x - Card.WIDTH / 4 * i, y);
			AddCard(cards[i]);
		}
		mValid = true;
	}
	
	public void InitFromAnchor(CardAnchor cardAnchor, float x, float y, int numCards) {
		mCardAnchor = cardAnchor;
		Card[] cards = cardAnchor.GetCardStack();
		//Log.i("[MoveCard]", "[InitFromAnchor] cards == " + String.valueOf(cards.length));

		for (int i = 0; i < numCards; i++) {
			cards[i].SetPosition(x - Card.WIDTH / 4 * i, y);
			AddCard(cards[i]);
		}
		mValid = true;
	}

  public boolean HasMoved() {
    float x = mCard[0].GetX();
    float y = mCard[0].GetY();

    if (x >= mOriginalPoint.x - 3 && x <= mOriginalPoint.x + 3 &&
        y >= mOriginalPoint.y - 3 && y <= mOriginalPoint.y + 3) {
      return false;
    }
    return true;
  }
}